/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import java.util.ArrayList;
import org.ipti.br.tag.entity.Actor_;
import org.ipti.br.tag.entity.Discipline_;
import org.ipti.br.tag.entity.Performance;
import org.ipti.br.tag.entity.Performance_;
import org.ipti.br.tag.entity.enums.Reference;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class PerformanceFacade extends AbstractFacade<Performance> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PerformanceFacade() {
        super(Performance.class);
    }

    @Override
    public void create(Performance performance) {
        getEntityManager().persist(performance);
    }

    public Integer gradesQuantityForUnityDiscipline(Long disciplineID,
            Reference reference, Long referenceID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery(Performance.class);
        Root<Performance> root = cq.from(Performance.class);
        cq.select(cb.max(root.get(Performance_.seq)));

        cq.where(cb.and(
                cb.equal(root.join(Performance_.discipline).get(Discipline_.id), disciplineID),
                cb.equal(root.get(Performance_.reference), reference),
                cb.equal(root.get(Performance_.referenceID), referenceID)));

        try {
            return ((Integer) getEntityManager().createQuery(cq).getSingleResult()).intValue();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Performance> actorGradesPerReferenceDiscipline(Long actorID,
            Long disciplineID, Reference reference, Long referenceID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Performance> cq = cb.createQuery(Performance.class);
        Root<Performance> root = cq.from(Performance.class);

        cq.where(cb.and(cb.equal(root.join(Performance_.actor).get(Actor_.id), actorID),
                cb.equal(root.join(Performance_.discipline).get(Discipline_.id), disciplineID),
                cb.equal(root.get(Performance_.reference), reference),
                cb.equal(root.get(Performance_.referenceID), referenceID)));

        cq.orderBy(cb.asc(root.get(Performance_.seq)));
        try {
            return getEntityManager().createQuery(cq).getResultList();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Performance> modifiedPerfFreqBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Performance> cq = cb.createQuery(Performance.class);
        Root<Performance> from = cq.from(Performance.class);
        Predicate predicate1 = cb.notEqual(from.get(Performance_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Performance_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Performance_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public Boolean hasPerfomances(Actor actor) {
       
        try {
            List<Performance> performanceList = (List<Performance>) em.createNativeQuery("select * from performance "
                    + "where actor_id = " + actor.getId()+ " and grade > 0", Performance.class).getResultList();
            if (performanceList == null || performanceList.isEmpty()) {
                return false;
            }
            return true;
        } catch (Exception ex) {
            return true;
        }
    }
}
